package io.mqttpush.common.events.utils;
import io.mqttpush.common.events.MsgEvent;
import io.mqttpush.common.events.OffLineEvent;
import io.mqttpush.common.events.OnLineEvent;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

import java.nio.charset.Charset;
import java.util.Date;

/**
 * 构建报文的工具类
 * @author tianzhenjiu
 *
 */
public class ByteBufEncodingUtil {


	private static ByteBufEncodingUtil  byteBufEncodingUtil=new ByteBufEncodingUtil();


	private ByteBufEncodingUtil(){}


	public  static ByteBufEncodingUtil getInstance() {
	    return byteBufEncodingUtil;
	}



	/**
	 * 编码上线消息
	 * @param event
	 * @param allocator
	 * @return
	 */
	public ByteBuf encodOnLineEvent(OnLineEvent event, ByteBufAllocator allocator){

		ByteBuf byteBuf=encodEvent(event,allocator);

		/**
		 * 写用户信息
		 */
		String userInfo =event.userInfo;
		byteBuf.writeInt(userInfo.length());
		byteBuf.writeCharSequence(userInfo, Charset.forName("utf-8"));



		return byteBuf;
	}


	/**
	 * 编码下线消息
	 * @param event
	 * @param allocator
	 * @return
	 */
	public ByteBuf encodOffLineEvent(OffLineEvent event, ByteBufAllocator allocator){



		/**
		 * 写用户信息
		 */
		String userInfo =event.userInfo;

		if(userInfo==null){
			return null;
		}

		ByteBuf byteBuf=encodEvent(event,allocator);
		byteBuf.writeInt(userInfo.length());
		byteBuf.writeCharSequence(userInfo, Charset.forName("utf-8"));


		/**
		 * 写原因
 		*/
		String offCause =event.offCause;
		if(offCause!=null&&offCause.length()>0){
			byteBuf.writeCharSequence(offCause, Charset.forName("utf-8"));
		}else{
			byteBuf.writeInt(0);
		}



		return byteBuf;
	}

	public ByteBuf encodEventCommon(MsgEvent event, ByteBufAllocator allocator){

		if(event instanceof OffLineEvent){
			return  encodOffLineEvent((OffLineEvent) event,allocator);
		}

		else if(event instanceof OnLineEvent){
			return  encodOnLineEvent((OnLineEvent) event,allocator);
		}


		return allocator.buffer();
	}

	public ByteBuf encodEvent(MsgEvent event, ByteBufAllocator allocator){

		ByteBuf byteBuf=allocator.buffer();

		/**
		 * 写入时间
		 */
		byteBuf.writeLong(event.eventTime.getTime());

		/**
		 * 写入事件名称
		 */
		String eventName =event.eventName();
		byteBuf.writeInt(eventName.length());
		byteBuf.writeCharSequence(eventName, Charset.forName("utf-8"));

		/**
		 * 写设备号
		 */
		String deviceId =event.deviceId;
		byteBuf.writeInt(deviceId.length());
		byteBuf.writeCharSequence(deviceId, Charset.forName("utf-8"));

		return byteBuf;

	}

	public OnLineEvent decodeOnLineEvent(ByteBuf byteBuf){

		OnLineEvent onLineEvent=new OnLineEvent();

		decodeEvent(byteBuf,onLineEvent);

		int contentLen=byteBuf.readInt();
		byte[] content=new byte[contentLen];
		byteBuf.readBytes(content);
		onLineEvent.userInfo=new String(content, Charset.forName("utf-8"));
		return onLineEvent;

	}


	public OffLineEvent decodeOffLineEvent(ByteBuf byteBuf){

		OffLineEvent offLineEvent=new OffLineEvent();

		decodeEvent(byteBuf,offLineEvent);

		int contentLen=byteBuf.readInt();
		byte[] content=new byte[contentLen];
		byteBuf.readBytes(content);
		offLineEvent.userInfo=new String(content, Charset.forName("utf-8"));


		contentLen=byteBuf.readInt();
		content=new byte[contentLen];
		byteBuf.readBytes(content);
		offLineEvent.offCause=new String(content, Charset.forName("utf-8"));

		return offLineEvent;

	}



	public void decodeEvent(ByteBuf byteBuf,MsgEvent event){

		long time=byteBuf.readLong();
		event.eventTime=new Date(time);


		int contentLen=byteBuf.readInt();
		byte[] content=new byte[contentLen];
		byteBuf.readBytes(content);
		String eventName=new String(content, Charset.forName("utf-8"));

		contentLen=byteBuf.readInt();
		content=new byte[contentLen];
		byteBuf.readBytes(content);
		event.deviceId=new String(content, Charset.forName("utf-8"));

	}
}
